//
//  HLTyijianbangkaTableViewController.m
//  huiliantong
//
//  Created by hlt on 15/9/14.
//  Copyright (c) 2015年 hlt. All rights reserved.
//

#import "HLTyijianbangkaTableViewController.h"
#import "HLTzidingyibangkaTableViewController.h"
@interface HLTyijianbangkaTableViewController ()<MONActivityIndicatorViewDelegate>

@property (weak, nonatomic) IBOutlet UILabel *cardno;


@property (weak, nonatomic) IBOutlet UILabel *liushuihao;

@property (weak, nonatomic) IBOutlet UILabel *pwd;



@property (weak, nonatomic) IBOutlet UILabel *yzm;



@end

@implementation HLTyijianbangkaTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.cardno.text=self.kahao;
    self.liushuihao.text=self.liushuihao1;
    self.pwd.text=self.pwd1;
    
    
    
    UIView *view1=[[UIView alloc] initWithFrame:self.view.bounds];
    view1.backgroundColor=[UIColor grayColor];
    view1.alpha=0.4;
    [self.view addSubview:view1];
    MONActivityIndicatorView *indicatorView = [[MONActivityIndicatorView alloc] init];
    indicatorView.delegate = self;
    indicatorView.numberOfCircles = 3;
    indicatorView.radius = 20;
    indicatorView.internalSpacing = 3;
    indicatorView.center = self.view.center;
    [indicatorView startAnimating];
    [self.view addSubview:indicatorView];
    
    
    //发送绑定卡得验证码
    
    Account *acc=[[AccountTool sharedAccountTool] currentAccount];
    NSString *url=[bangdingkayanzhengma stringByAppendingFormat:@"&user_name=%@&cardNum=%@",acc.custPhone,self.kahao];
    
    [HLTNetWorkingtool getdic:url success:^(NSDictionary *ary) {
        [indicatorView stopAnimating];
        [view1 removeFromSuperview];
        //将nsnumber 装换成nsstring
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString *str = [numberFormatter stringFromNumber:ary[@"code"]];
        if ([str isEqual:@"2"]) {
            
            //重新赋值流水号
            self.liushuihao.text=ary[@"bandHuiRequestSvrTId"];
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"验证码短信已经发送"   message:ary[@"msg"]
                                                        delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
            
            [alert show];
            
        }else
        {
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"验证码短信发送失败"   message:ary[@"msg"]
                                                        delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
            [alert show];
            
            
        }
        

        
    } faliure:^(NSString *error) {
        
        
        [indicatorView stopAnimating];
        [view1 removeFromSuperview];
        
        
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"验证码短信发送失败"   message:nil
                                                    delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
        [alert show];
        
        
    }];
    
    
    
    
    
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        if (indexPath.row==2)
        {
            
            
            UIView *view1=[[UIView alloc] initWithFrame:self.view.bounds];
            view1.backgroundColor=[UIColor grayColor];
            view1.alpha=0.4;
            [self.view addSubview:view1];
            MONActivityIndicatorView *indicatorView = [[MONActivityIndicatorView alloc] init];
            indicatorView.delegate = self;
            indicatorView.numberOfCircles = 3;
            indicatorView.radius = 20;
            indicatorView.internalSpacing = 3;
            indicatorView.center = self.view.center;
            [indicatorView startAnimating];
            [self.view addSubview:indicatorView];
            
            //执行发送卡密码的请求
            Account *acc=[[AccountTool sharedAccountTool] currentAccount];
            NSString *url=[kamima stringByAppendingFormat:@"&password=%@&mobile=%@",self.pwd1,acc.custPhone];
            
            [HLTNetWorkingtool getdic:url success:^(NSDictionary *ary) {
                
                [indicatorView stopAnimating];
                [view1 removeFromSuperview];
                
                //将nsnumber 装换成nsstring
                NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
                NSString *str = [numberFormatter stringFromNumber:ary[@"code"]];
                if ([str isEqual:@"2"]) {
                    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"短信发送成功"   message:ary[@"msg"]
                                                                delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
                    
                    [alert show];
                    
                }
            } faliure:^(NSString *error) {
                
                [indicatorView stopAnimating];
                [view1 removeFromSuperview];
                
            }];

            
        
        }
        else if (indexPath.row==3)
        {
            [EYInputPopupView popViewWithTitlechar:@"填写验证码" contentText:@"请填写验证码"
                                                type:EYInputPopupView_Type_single_line_text
                                         cancelBlock:^{
                                             
                                         } confirmBlock:^(UIView *view, NSString *text) {
                                             
                                             self.yzm.text=text;
//                                             self.newpwd.text=text;
                                         } dismissBlock:^{
                                             
                                         }];
        }
    }
    
    
    
    
    
}






- (IBAction)bangdingqueren:(id)sender {
    
    UIView *view1=[[UIView alloc] initWithFrame:self.view.bounds];
    view1.backgroundColor=[UIColor grayColor];
    view1.alpha=0.4;
    [self.view addSubview:view1];
    MONActivityIndicatorView *indicatorView = [[MONActivityIndicatorView alloc] init];
    indicatorView.delegate = self;
    indicatorView.numberOfCircles = 3;
    indicatorView.radius = 20;
    indicatorView.internalSpacing = 3;
    indicatorView.center = self.view.center;
    [indicatorView startAnimating];
    [self.view addSubview:indicatorView];
    
    Account *acc=[[AccountTool sharedAccountTool] currentAccount];
    NSString *url=[querenbangding stringByAppendingFormat:@"&user_name=%@&cardNum=%@&password=%@&mobile_code=%@&transactionid=%@",acc.custPhone,self.kahao,self.pwd1,self.yzm.text,self.liushuihao.text];
    
    [HLTNetWorkingtool getdic:url success:^(NSDictionary *ary) {
        
       
        //将nsnumber 装换成nsstring
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString *str = [numberFormatter stringFromNumber:ary[@"code"]];
        if ([str isEqual:@"2"]) {
            
            
            
            
            
            NSString *url=[chaxunyue stringByAppendingFormat:@"&user_name=%@&cardNum=%@",acc.custPhone,self.kahao];
            [HLTNetWorkingtool getdic:url success:^(NSDictionary *ary) {
                [indicatorView stopAnimating];
                [view1 removeFromSuperview];
                
                if ([ary[@"status"] isEqual:@"0"]) {
//                    Account *acc=[[AccountTool sharedAccountTool] currentAccount];
//
                    acc.cardNo=self.kahao;
                    acc.YKTCardBalance=ary[@"deposit"];
                    [[AccountTool sharedAccountTool] addAccounts:acc];
                    NSLog(@"%@",ary);
                    [self.navigationController popViewControllerAnimated:YES];
                    [indicatorView stopAnimating];
                    [view1 removeFromSuperview];
                    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"绑定卡成功"   message:ary[@"msg"]
                                                                delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
                    
                    [alert show];
                }
            } faliure:^(NSString *error) {
                [indicatorView stopAnimating];
                [view1 removeFromSuperview];
                
            }];

            
            
            
            
            
            
//            [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:2] animated:YES];
            
//            [self.navigationController popViewControllerAnimated:YES]; //返回上一页面
            
        }else
        {
            
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"绑定卡失败（如果没有通过手机端修改卡密码，直接到客服处修改密码会导致一键绑定卡失败，需要将卡密码修改为原始密码之后才能绑定成功）"   message:ary[@"msg"]
                                                        delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
            
            [alert show];
            [indicatorView stopAnimating];
            [view1 removeFromSuperview];
            
            
        }
        
        
        
    } faliure:^(NSString *error) {
        //这个是停止加载页面
        [indicatorView stopAnimating];
        [view1 removeFromSuperview];
    }];
    
    
    
    
    
}


- (IBAction)zidingyibangding:(id)sender {
    
    [EYInputPopupView popViewWithTitle:@"请输入卡号" contentText:@"卡号"
                                  type:EYInputPopupView_Type_single_line_text
                           cancelBlock:^{
                               
                           } confirmBlock:^(UIView *view, NSString *text) {
                               
                               
                               
                               UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                               HLTzidingyibangkaTableViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:@"HLTzidingyibangkaTableViewController"];
                               vc.kahao=text;
                               [self.navigationController pushViewController:vc animated:NO];
                               
                           } dismissBlock:^{
                               
                           }];

    
    
    
    
    
    
    
}






@end
